local filename = "downclientcfg.lua"
local msghelper = require "agenthelper"
local configdao = require "configdao"
local msgpack = require "MessagePack"
local filelog = require "filelog"
local json = require "cjson"
json.encode_sparse_array(true,1,1)
require "enum"

local DownClientCfg = {}

function DownClientCfg.process(session, source, fd, request)
    local responsemsg = {
        errcode = EErrCode.ERR_SUCCESS,
    }
    local server = msghelper:get_server()

    --检查当前登陆状态
    if not msghelper:is_login_success() then
        filelog.sys_warning("Doaction.process invalid server state", server.state)
        responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "无效的请求！"
        msghelper:send_resmsgto_client(fd, "DownloadCfgRes", responsemsg)
        return
    end
    if request.resconfinfos == nil then
        responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "请求参数无效"
        msghelper:send_resmsgto_client(fd, "DownloadCfgRes", responsemsg)
        return
    end

    responsemsg.reqconfinfos = {}
    for _,value in pairs(request.resconfinfos) do
        if request.version.clienttype == 1 then
            local config,confchtime = configdao.get_business_source(request.version.platform, request.version.channel, value.confname)
            if config and confchtime ~= value.changetime then
                local baseTab = {}
                baseTab.changetime = confchtime
                baseTab.confname = value.confname
                baseTab.confcontent = msgpack.pack(config)
                table.insert(responsemsg.reqconfinfos, baseTab)
            end
        else
            local getconfstatus, getconfsall = configdao.deepcopy_business_conf(request.version.platform,request.version.channel,value.confname)
            if getconfsall and getconfsall.changetime and getconfsall.changetime ~= value.changetime then
                local baseTab = {}
                baseTab.changetime = getconfsall.changetime
                baseTab.confname = value.confname
                local confjson = json.encode(getconfsall.conf)
                baseTab.confcontent = confjson
                table.insert(responsemsg.reqconfinfos, baseTab)
            end
        end
    end

    msghelper:send_resmsgto_client(fd, "DownloadCfgRes", responsemsg)
end

return DownClientCfg


